﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;

namespace TStart
{
    public partial class frmEdit : Form
    {
        public frmEdit()
        {
            InitializeComponent();

            labelIcon.Tag = "";
            this.TopMost = true;
        }

        private bool PathExists(string path)
        {
            if(File.Exists(path))
            {
                return true;
            }

            if (Directory.Exists(path))
            {
                return true;
            }

            return false;
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            AppsItem item = AppsItemGet();

            if ((item.Name.Length == 0)
                || (!PathExists(item.Path))
                || (!PathExists(item.Icon)))
            {
                MessageBoxEx.Show(this, "请检查输入信息！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult = DialogResult.OK;
        }

        public void AppsItemSet(AppsItem item)
        {
            textBoxName.Text = item.Name;
            labelIcon.Tag = item.Icon;
            labelIcon.Image = GetSystemIcon.GetIconByFileName(item.Icon, true).ToBitmap();
            textBoxPath.Text = item.Path;
            textBoxArgs.Text = item.Args;
        }

        public AppsItem AppsItemGet()
        {
            AppsItem item = new AppsItem();

            item.Name = textBoxName.Text.Trim();
            item.Icon = ((string)labelIcon.Tag).Trim();
            item.Path = textBoxPath.Text.Trim();
            item.Args = textBoxArgs.Text.Trim();

            return item;
        }

        private void ButtonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择文件";
            dialog.Filter = "所有文件(*.*)|*.*";
            dialog.FileName = Misc.PathFileNameEx(textBoxPath.Text);
            dialog.InitialDirectory = Misc.PathFilePath(textBoxPath.Text);
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                AppsItemSet(Misc.PathAppsItem(dialog.FileName));
            }
        }

        private void ButtonFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            dialog.SelectedPath = Misc.PathFilePath(textBoxPath.Text);
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                AppsItemSet(Misc.PathAppsItem(dialog.SelectedPath));
            }
        }

        private void ButtonIconModify_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择文件";
            dialog.Filter = "所有文件(*.*)|*.*";
            dialog.FileName = Misc.PathFileNameEx((string)labelIcon.Tag);
            dialog.InitialDirectory = Misc.PathFilePath((string)labelIcon.Tag);
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                labelIcon.Tag = dialog.FileName;
                labelIcon.Image = GetSystemIcon.GetIconByFileName((string)labelIcon.Tag, true).ToBitmap();
            }
        }
    }
}
